package com.istock.state.demo.service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.istock.state.demo.constants.ChildOrderStatusEnum;
import com.istock.state.demo.dao.OrderPayMapper;
import com.istock.state.demo.model.OrderPay;
import com.istock.state.demo.model.OrderPayExample;

@Repository("orderPayService")
public class OrderPayService {

	@Autowired
	private OrderPayMapper orderPayMapper;
	
	public OrderPay createPay(Long orderId , BigDecimal amount) {
		OrderPay result = new OrderPay();
		result.setOrderId(orderId);
		result.setAmount(amount);
		result.setStatus(ChildOrderStatusEnum.WAIT.getValue());
		result.setUpdateTime(new Date());
		orderPayMapper.insertSelective(result);
		return result;
	} 
	
	public List<OrderPay> findExpireOrderPay(){
		OrderPayExample example = new OrderPayExample();
		OrderPayExample.Criteria criteria = example.createCriteria();
		
		criteria.andUpdateTimeBetween(DateUtils.addMinutes(new Date(), -30),DateUtils.addSeconds(new Date(), -1));
		criteria.andStatusIn(Arrays.asList(ChildOrderStatusEnum.WAIT.getValue() ,ChildOrderStatusEnum.PROCESSING.getValue()));
		//这边为了防止单次查询的数据量过多,可以增加orderBy和分页
		return orderPayMapper.selectByExample(example);
	}
	
	public OrderPay findOrderPayById(Long id) {
		return orderPayMapper.selectByPrimaryKey(id);
	}
}
